package xuperchain

import (
	"encoding/json"
	"fmt"

	"github.com/xuperchain/xuper-sdk-go/v2/account"
	"github.com/xuperchain/xuper-sdk-go/v2/xuper"
)

func GetUserPermissionLevel(papername string, username string) string {

	type PermissionLevel struct {
		Level string `json:"0"`
	}
	account, err := account.GetAccountFromFile(path, pwd)
	if err != nil {
		panic(err)
	}
	fmt.Printf("privatekey Account address: %v\n", account.Address)

	err = account.SetContractAccount(contractAccount)
	if err != nil {
		panic(err)
	}

	xchainClient, err := xuper.New(nodeAddress)
	if err != nil {
		panic(err)
	}
	contractName := "xc_filestorage8"
	//获取访问记录长度
	args := map[string]string{
		"_name":     papername,
		"_username": username,
	}

	txGetFile, err := xchainClient.QueryEVMContract(account, contractName, "getUserPermissionLevel", args)
	if err != nil {
		panic(err)
	}
	var response []PermissionLevel
	err = json.Unmarshal([]byte(txGetFile.ContractResponse.Body), &response)
	if err != nil {
		panic(err)
	}

	return response[0].Level
}
